package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 客户消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:41
 */
public class CustomerAttrPushSet extends AbstractPushSet {


    private String pushObject;

    private String pushTime;

    private String attrEntity;

    private static final Integer ONE_DAY = 86400;
    private static final Integer THREE_DAY = 3*86400;
    private static final Integer FIVE_DAY = 5*86400;
    private static final Integer SEVEN_DAY = 7*86400;
    private static final Integer FIFTEEN_DAY = 15*86400;

    private static final Integer ONE_DAY_TYPE = 1;
    private static final Integer THREE_DAY_TYPE = 2;
    private static final Integer FIVE_DAY_TYPE = 3;
    private static final Integer SEVEN_DAY_TYPE = 4;
    private static final Integer FIFTEEN_DAY_TYPE = 5;

    public String getPushObject() {
        return pushObject;
    }



    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    public String getPushTime() {
        return pushTime;
    }



    public void setPushTime(String pushTime) {
        this.pushTime = pushTime;
    }



    public String getAttrEntity() {
        return attrEntity;
    }



    public void setAttrEntity(String attrEntity) {
        this.attrEntity = attrEntity;
    }



    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,
                                         String attrEntity) {
        setPushObject(pushObject);
        setPushTime(pushTime);
        setAttrEntity(attrEntity);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,
                                    PushSetModel pushSetModel) {
        return null;
    }
    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        Integer type = PushTypeEnum.CUSTOMER_REMIND.getCode();

        Map<String,Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("attrEntities", attrList);
        param.put("templateId", templateId);
        param.put("del", 0);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        Set<String> collect = pushSetList.stream().map(PushSetEntity::getAttrEntity).collect(Collectors.toSet());
        attrList.removeAll(collect);
        pushSetList.forEach(v -> v.setContent(I18nMessageUtil.getMessage(PushSetConstant.CUSTOMER_ATTR)));

        for (String attr : attrList) {
            PushSetEntity pushSetEntity = new PushSetEntity();
            pushSetEntity.setTemplateId(templateId);
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.CUSTOMER_ATTR));
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(PushTypeEnum.CUSTOMER_REMIND.getSubCode());
            pushSetEntity.setAttrEntity(attr);
            JSONObject pushObject = new JSONObject();
            /************推送对象设置*************/
            //type
            JSONArray typeArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", PushObjectTypeEnum.MAINUSER.getName());
            jsonObject.put("enable", 0);
            jsonObject.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
            typeArray.add(jsonObject);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
            jsonObject1.put("enable", 0);
            jsonObject1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
            typeArray.add(jsonObject1);
            pushObject.put("type", typeArray);
            //manager
            JSONObject managerObject = new JSONObject();
            managerObject.put("enable", 0);
            managerObject.put("value", new ArrayList<>());
            pushObject.put("manager", managerObject);
            //dep
            JSONObject depObject = new JSONObject();
            depObject.put("enable", 0);
            depObject.put("value", new ArrayList<>());
            pushObject.put("dep", depObject);
            //role
            JSONObject roleObject = new JSONObject();
            roleObject.put("enable", 0);
            roleObject.put("value", new ArrayList<>());
            pushObject.put("role", roleObject);
            //user
            JSONObject userObject = new JSONObject();
            userObject.put("enable", 0);
            userObject.put("value", new ArrayList<>());
            pushObject.put("user", userObject);
            pushSetEntity.setPushObject(pushObject.toJSONString());
            /*************推送时间设置***********/
            JSONObject pushTimeObject = new JSONObject();
            //提前推送
            pushTimeObject.put("timeType", PushTimeTypeEnum.ADVANCE.getCode());
            //默认
            pushTimeObject.put("time", 0);
            //无效时间间隔
            pushTimeObject.put("period", ONE_DAY);
            //循环
            pushTimeObject.put("loop", 0);
            //时间控件类型 0具体时间点 1天数
            pushTimeObject.put("type", 1);

            JSONArray timeArray = new JSONArray();
            JSONObject timeObject = new JSONObject();
            timeObject.put("id", 1);
            timeObject.put("name", I18nMessageUtil.getMessage(CommonConstant.ADVANCE));
            timeArray.add(timeObject);
            JSONObject timeObject1 = new JSONObject();
            timeObject1.put("id", 2);
            timeObject1.put("name", I18nMessageUtil.getMessage(CommonConstant.DELAYED));
            timeArray.add(timeObject1);
            JSONObject timeObject2 = new JSONObject();
            timeObject2.put("id", 3);
            timeObject2.put("name", I18nMessageUtil.getMessage(CommonConstant.THE_DAY));
            timeArray.add(timeObject2);
            pushTimeObject.put("timeArray", timeArray);

            pushSetEntity.setPushTime(pushTimeObject.toJSONString());
            pushSetEntity.setEnable(0);

            pushSetList.add(pushSetEntity);
        }

        return pushSetList;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        Integer timeType = PushTimeTypeEnum.ADVANCE.getCode();
        Integer time = -1;
        Integer period = 0;
        Integer timeIntervalType = 0;
        Integer loop = 0;
        Integer pushTimeType = jsonObject.getInteger("pushTimeType");
        if(Objects.equals(pushTimeType, ONE_DAY_TYPE)){
            period = ONE_DAY;
        }else if(Objects.equals(pushTimeType, THREE_DAY_TYPE)){
            period = THREE_DAY;
        }else if(Objects.equals(pushTimeType, FIVE_DAY_TYPE)){
            period = FIVE_DAY;
        }else if(Objects.equals(pushTimeType, SEVEN_DAY_TYPE)){
            period = SEVEN_DAY;
        }else if(Objects.equals(pushTimeType, FIFTEEN_DAY_TYPE)){
            period = FIFTEEN_DAY;
        }
        JSONObject retObject = new JSONObject();
        retObject.put("timeType", timeType);
        retObject.put("time", time);
        retObject.put("period", period);
        retObject.put("timeIntervalType", timeIntervalType);
        retObject.put("loop", loop);
        return retObject;
    }


}
